﻿using System.Collections.Generic;

namespace System.Collections
{
    /// <summary>
    /// 
    /// </summary>
    public static class IEnumeratorExtensions
    {
        /// <summary>
        /// Convert enumerator to List
        /// </summary>
        /// <param name="en">the enumerator</param>
        /// <returns></returns>
        public static List<T> ToList<T>(this IEnumerator en)
        {
            Check.Require(en, "en");

            List<T> list = new List<T>();

            while (en.MoveNext())
            {
                list.Add((T)en.Current);
            }
            return list;
        }

        /// <summary>
        /// Check equal of two arrays
        /// </summary>
        /// <param name="enLeft">left array</param>
        /// <param name="enRight">right array</param>
        /// <returns></returns>
        public static bool IsArrayEquals(this IEnumerator enLeft, IEnumerator enRight)
        {
            if ((enLeft == null && enRight != null) ||
                (enLeft != null && enRight == null))
            {
                return false;
            }

            bool isEquals = true;

            while (true)
            {
                bool leftHasNext = enLeft.MoveNext();
                bool rightHasNext = enRight.MoveNext();

                if ((leftHasNext && (!rightHasNext)) ||
                    ((!leftHasNext) && rightHasNext))
                {
                    isEquals = false;
                    break;
                }

                if ((!leftHasNext) && (!rightHasNext))
                {
                    break;
                }

                if ((enLeft.Current == null && enRight.Current != null) ||
                    (enLeft.Current != null && enRight.Current == null) ||
                    (!enLeft.Current.Equals(enRight.Current)))
                {
                    isEquals = false;
                    break;
                }
            }

            return isEquals;
        }

    }
}
